
package com.ldzhao.blog.config;

import com.ldzhao.blog.model.entity.ArticleContent;
import com.ldzhao.blog.service.BizArticleContentService;
import com.ldzhao.blog.spider.service.SyncNavInfoService;
import com.ldzhao.blog.spider.service.SyncOpenSourceInfoService;
import com.ldzhao.blog.spider.service.SyncToolInfoService;
import com.ldzhao.blog.util.AliyunOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 异步线程配置
 *
 * @author ldzhao
 * @version 1.0
 * @date 2018/4/28 11:04
 * @since 1.0
 */
@Slf4j
public class AsyncTaskConfig {
    @Autowired
    private SyncOpenSourceInfoService syncOpenSourceInfoService;
    @Autowired
    private SyncToolInfoService syncToolInfoService;
    @Autowired
    private SyncNavInfoService syncNavInfoService;
    @Autowired
    private BizArticleContentService bizArticleContentService;
    @Value("${blog.content.url}")
    private String blogContentUrl;

    @Scheduled(cron = "0 0 23 * * ?")
    private void syncGiteeOpenSource() {
        try {
            syncOpenSourceInfoService.syncGiteeOpenSources();
        } catch (Exception e) {
            log.error("syncOpenSourceInfoService.syncGiteeOpenSources", e);
        }
    }

    @Scheduled(cron = "0 0 23 ? * SUN")
    private void syncGithubOpenSource() {
        try {
            syncOpenSourceInfoService.syncGithubOpenSources();
        } catch (Exception e) {
            log.error("syncOpenSourceInfoService.syncGithubOpenSources", e);
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    private void syncTool() {
        try {
            syncToolInfoService.syncTools();
        } catch (Exception e) {
            log.error("syncToolInfoService.syncTools", e);
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    private void syncNav() {
        try {
            syncNavInfoService.syncNavs();
        } catch (Exception e) {
            log.error("syncNavInfoService.syncNavs", e);
        }
    }


    @PostConstruct
    private void init() {
//        syncGiteeOpenSource();
//        syncGithubOpenSource();
//        syncTool();
//        syncNavInfoService.syncNavs();
        List<ArticleContent> articleContents = bizArticleContentService.listAll();
        if (!CollectionUtils.isEmpty(articleContents)) {
            articleContents.forEach(x -> {
                try {
                    if ((!StringUtils.isEmpty(x.getContent()) && x.getContent().contains(blogContentUrl)) || (!StringUtils.isEmpty(x.getContentMd()) && x.getContentMd().contains(blogContentUrl))) {
                        return;
                    }
                    boolean upload = AliyunOSSUtil.upload(x);
                    if (upload) {
                        if (!StringUtils.isEmpty(x.getContent())) {
                            x.setContent(blogContentUrl + "/html/" + x.getId());
                        }
                        if (!StringUtils.isEmpty(x.getContentMd())) {
                            x.setContentMd(blogContentUrl + "/markdown/" + x.getId());
                        }
                        bizArticleContentService.updateSelective(x);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
